/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package it.davial.mgwt.client.activities.picture;

import it.davial.mgwt.client.Area;
import it.davial.mgwt.client.ClientFactory;
import it.davial.mgwt.client.Patient;
import it.davial.mgwt.client.activities.BaseActivity;
import it.davial.mgwt.client.activities.patient.PatientView;
import it.davial.mgwt.client.event.ActionEvent;
import it.davial.mgwt.client.event.ActionNames;
import it.davial.mgwt.client.service.DbServiceAsync;

import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.HTML;
import com.googlecode.mgwt.dom.client.event.tap.TapEvent;
import com.googlecode.mgwt.dom.client.event.tap.TapHandler;

/**
 * Activities are started and stopped by an ActivityManager associated with a container Widget.
 */
public class PictureActivity extends BaseActivity implements PictureView.Presenter {
	private static final HTML TITLE = new HTML("Picture"); 
	private static final int ID_FIGURE_ROOT  = 1; 
	public PictureActivity(ClientFactory clientFactory) {
		super(clientFactory.getPictureView(), "nav");
		this.clientFactory = clientFactory;
	}

	private static final Logger logger = Logger.getLogger("MainActivity");

	/**
	 * Used to obtain views, eventBus, placeController.
	 * Alternatively, could be injected via GIN.
	 */
	private ClientFactory clientFactory;

	/**
	 * Sample property.
	 */

	
	@Override
	public void start(AcceptsOneWidget containerWidget, final EventBus eventBus) {
		logger.info("starting MainActivity");
		PictureView view = clientFactory.getPictureView();
		view.getBackbuttonText().setText("Home");

		view.getHeader().setCenterWidget(TITLE);

		view.getMainButtonText().setText("Nav");

		view.setPresenter(this);

		addHandlerRegistration(view.getBackbutton().addTapHandler(new TapHandler() {

			@Override
			public void onTap(TapEvent event) {
				ActionEvent.fire(eventBus, ActionNames.BACK);

			}
		}));

		containerWidget.setWidget(view.asWidget());
		fillBodyArea(ID_FIGURE_ROOT,view);
	}

	
	public void fillBodyArea(final int idFigure,final PictureView view){
		logger.info("starting MainActivity");
		DbServiceAsync.Util.getInstance().findAreaById(idFigure,new AsyncCallback<List<Area>>() {
			
			@Override
			public void onSuccess(List<Area> result) {
				
				logger.info("success, result=" + result);
				view.setBodyArea(idFigure,result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				logger.info("Error" + caught );
				
			}
		});
	}
	
/*	@Override
	public String mayStop() {
		return "Please hold on. This activity is stopping.";
	}
*/
	/**
	 * @see PictureView.Presenter#goTo(Place)
	 */
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}
}
